package fr.playsoft.psas;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

public class FakeActionSheet extends Activity {
    
	private Button btnShowPopUp;
    private PopupWindow mpopup;
    
	/** Called when the activity is first created. */
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
				
		setContentView(R.layout.actionsheet_main);
		
		setupPopupWindow();
	}
	
	private void setupPopupWindow() {
		btnShowPopUp = (Button)findViewById(R.id.btnShowPopUp);
        
        btnShowPopUp.setOnClickListener(new OnClickListener() 
        {            
            @Override
            public void onClick(View arg0) 
            {
                View popUpView = getLayoutInflater().inflate(R.layout.actionsheet_popup, null); // inflating popup layout
                mpopup = new PopupWindow(popUpView, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, true); //Creation of popup
                mpopup.setAnimationStyle(android.R.style.Animation_Dialog);   
                mpopup.showAtLocation(popUpView, Gravity.BOTTOM, 0, 0);    // Displaying popup
                
                mpopup.setTouchInterceptor(new OnTouchListener() {
        			@Override
        			public boolean onTouch(View v, MotionEvent event) {
        				if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
        					mpopup.dismiss();

        					return true;
        				}

        				return false;
        			}
        		});
                
                Button btnOk = (Button)popUpView.findViewById(R.id.btnOk);
                btnOk.setOnClickListener(new OnClickListener() 
                {                    
                    @Override
                    public void onClick(View v) 
                    {
                        mpopup.dismiss();  //dismissing the popup
                    }
                });
                
                Button btnCancel = (Button)popUpView.findViewById(R.id.btnCancel);
                btnCancel.setOnClickListener(new OnClickListener() 
                {                    
                    @Override
                    public void onClick(View v) 
                    {
                        mpopup.dismiss(); //dismissing the popup
                    }
                });
            }
        });  
		
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) 
	{
		if(keyCode==KeyEvent.KEYCODE_BACK && mpopup.isShowing()) {
			mpopup.dismiss();
			return true;
		}
		return super.onKeyDown(keyCode, event);
    }

}